﻿--[[	$Id: caelEmote.lua 2457 2011-12-22 10:56:38Z sdkyron@gmail.com $	]]

local _, caelEmote = ...

caelEmote.eventFrame = CreateFrame("Frame", nil, UIParent)

local locale = caelLib.locale
local playerFaction, oldState
local targets = {}

local randomEmote = {
	[1]  = "GLOAT",
	[2]  = "GRIN",
	[3]  = "CACKLE",
	[4]  = "GLARE",
	[5]  = "MOURN",
	[6]  = "GRIN",
}

local msg = locale == "frFR" and "a fait couler le premier sang" or "drew first blood"
local blow = GetLocale() == "frFR" and "COUP FATAL" or "KILLING BLOW"
--local welcome = GetLocale() == "frFR" and "Bienvenue dans World of Warcraft avec caelUI" or "Welcome to World of Warcraft with CaelUI"

caelEmote.eventFrame:SetScript("OnEvent", function(self, event, ...)
	if event == "ADDON_LOADED" then
		local addon = ...

		if addon ~= "caelEmote" then
			return
		end

		if not caelEmoteDB then
			caelEmoteDB  = {}
		end

		self:UnregisterEvent("ADDON_LOADED")
	elseif event == "PLAYER_ENTERING_WORLD" then
--		if GetNumTalentTabs() == 0 then
--			recScrollAreas:AddText("|cffD7BEA5"..welcome.."|r", true, "Error")
--		end

		playerFaction = UnitFactionGroup("player")
		self:UnregisterEvent("PLAYER_ENTERING_WORLD")
	elseif event == "ZONE_CHANGED_NEW_AREA" then
		local _, instanceType = IsInInstance()

		if instanceType == "arena" or instanceType == "pvp" then
			caelEmoteDB.lastKill = 0
			PlaySoundFile(caelMedia.files.soundEnteringPvPZone)
		end
	elseif event == "COMBAT_LOG_EVENT_UNFILTERED" then
		local _, subevent, _, _, sourceName, _, _, _, destName, _, _, spellId = ...

		if subevent == "PARTY_KILL" then
			local dashPos = destName:find("-")
			if dashPos then
				destName = destName:sub(0, dashPos - 1)
			end
			if targets[destName] then
				if sourceName == caelLib.playerName then
					DoEmote(randomEmote[math.random(1, 6)], destName)
					PlaySoundFile(caelMedia.files.soundGodlike)
					recScrollAreas:AddText("|cffAF5050"..blow.."|r", true, "Notification")

					if caelEmoteDB.lastKill == 0 then
						SendChatMessage(msg, "EMOTE", GetDefaultLanguage("player"))
						caelEmoteDB.lastKill = 1
					end
				end
				targets[destName] = nil
			end
		elseif subevent == "SPELL_CAST_SUCCESS" then
			if sourceName == caelLib.playerName then
				if spellId == 69041 then
					local sex = UnitSex("player")

                    local message = {
                        [2] = "montre son gros missile à ", -- Male
                        [3] = "montre son gros missile à ", -- Female
                    }

                    if sex ~= 1 then
                        SendChatMessage((message[sex]..destName), "EMOTE", GetDefaultLanguage("player"))
                    end
				end
			end
		end
	elseif event == "PLAYER_TARGET_CHANGED" then
		if UnitExists("target") and UnitIsPlayer("target") and UnitIsEnemy("player", "target") and UnitFactionGroup("target") ~= playerFaction then
			targets[UnitName("target")] = true
		end
	elseif event == "UNIT_AURA" then
		local unit, target = ...
		if  unit == "player" then
			local newState = UnitBuff(unit, "Lifeblood")
			if newState and not oldState then
				DoEmote("GROWL", target)
			end
			oldState = newState
		end
	end
end)

for _, event in next, {
	"ADDON_LOADED",
	"COMBAT_LOG_EVENT_UNFILTERED",
	"PLAYER_ENTERING_WORLD",
	"PLAYER_TARGET_CHANGED",
	"UNIT_AURA",
	"ZONE_CHANGED_NEW_AREA"
} do
	caelEmote.eventFrame:RegisterEvent(event)
end